# -*- coding: utf-8 -*-
# @Time : 2018/12/25 9:34
# @Author : Administrator
# @File : WoniuSalesV1.py
# @Project : python_demo
# @title :
import http.client,random,unittest,HTMLTestRunner,requests

from py41.http_test.read_data import Common


class Httplib_Test():
#
    def __init__(self):
        self.cookie=''#初始化cookie
        self.url='192.168.2.184'#初始化主机
        self.port=8080#初始化端口
        self.url_login='/woniusales/user/login'#初始化登录的URI资源
        self.header={'Content-Type':'application/x-www-form-urlencoded'}#构建头部
        self.con=http.client.HTTPConnection(self.url,self.port)#建立连接
        self.logindata='username=admin&password=admin123&verifycode=0000'#构建登录的正文数据
        self.con.request('POST',self.url_login,self.logindata,self.header)#发送登录的请求
        # res=self.con.getresponse().read().decode()
        # print(res)
        self.cookie=self.con.getresponse().getheader('Set-Cookie').split(';')[0]
        print(self.cookie)
        self.custadd_url='/woniusales/customer/add'
        # self.con.close()


#合法录入手机号未注册，注册会员
    def testcustomer_add01(self,customername,childsex,childdate,creditkids,creditcloth,expect):

        num = random.randint(10000000, 99999999)#随机数
        data_01=("customername="+customername+"&customerphone=131"+str(num)+"&childsex="+childsex+"&childdate="+childdate+"&creditkids="+creditkids+"&creditcloth="+creditcloth).encode()#构建新增会员信息的正文数据
        header={'Content-Type':'application/x-www-form-urlencoded','Cookie':self.cookie}#构建头信息
        print(self.cookie)
        self.con = http.client.HTTPConnection(self.url, self.port)#建立连接
        self.con.request('POST',self.custadd_url,data_01,header)#发送新增会员的请求
        res01=self.con.getresponse().read().decode()#获取新增会员接口的响应结果
        print(res01)
        assert res01==expect#响应断言
        # self.assertEqual(res01,expect)#响应断言
    #     self.con.close()
#
    def testcustomer_add02(self):#已注册手机号重复注册会员
        pass
#         data_01=('customername=lency&customerphone=18530785881'\
#                 '&childsex=女&childdate=2018-11-12&creditkids=0&creditcloth=0').encode()
#         header={'Content-Type':'application/x-www-form-urlencoded','Cookie':self.cookie}
#         self.con = http.client.HTTPConnection(self.url, self.port)
#         self.con.request('POST',self.custadd_url,data_01,header)
#         res02=self.con.getresponse().read().decode()
#         print(res02)
#         assert res02=='already-added'
#         self.con.close()
if __name__ == '__main__':
    Httplib_Test()
    # suite = unittest.TestSuite()
    # suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Httplib_Test))
    # with open('test_report.html', 'w', encoding='utf-8') as file:
    #     HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2,
    #                                   title='Httplib库-接口测试',
    #                                   description='会员管理模块接口测试').run(suite)

